package com.ldc.common.utils;

import android.app.Activity;
import android.text.Editable;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.google.android.material.textfield.TextInputEditText;
import com.ldc.common.common.CommonRowClick;
import com.ldc.common.common.CommonViewClick;

public class ViewUtils {

    /**
     * @param activity 活动
     * @param id       id
     * @param <T>      类型
     * @return View
     */
    public static <T extends View> T findBy(Activity activity, @IdRes int id) {
        if (null == activity) {
            return null;
        }
        return activity.findViewById(id);
    }

    /**
     * @param view 视图
     * @param id   id
     * @param <T>  类型
     * @return View
     */
    public static <T extends View> T findBy(View view, @IdRes int id) {
        return view.findViewById(id);
    }

    /**
     * @param activity
     * @param id
     * @param stringRes
     */
    public static void setText(Activity activity, @IdRes int id, @StringRes int stringRes) {
        String text = activity.getResources().getString(stringRes);
        setText(activity, id, text);
    }

    /**
     * @param activity
     * @param id
     * @param text
     */
    public static void setText(Activity activity, @IdRes int id, String text) {
        View tv = findBy(activity, id);
        if (tv instanceof TextView) {
            ((TextView) tv).setText(text);
        }

    }

    public static void addViewClick(Activity activity, @IdRes int id, CommonViewClick<View> clickListener) {
        View view = findBy(activity, id);
        if (view == null) {
            return;
        }
        view.setOnClickListener(v -> {
            if (null != clickListener) {
                clickListener.onRow(v);
            }
        });
    }


    public static void setVisibility(Activity activity, @IdRes int id, boolean visible) {
        setVisibility(findBy(activity, id), visible);
    }

    public static void setVisibility(View view, boolean visible) {
        if (view == null) return;
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public static void setVisibility(View root, @IdRes int id, boolean visible) {
        View view = findBy(root, id);
        if (view == null) return;
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
    }


    public static String getText(Activity activity, @IdRes int id) {
        View view = findBy(activity, id);
        if (view instanceof TextView) {
            return (String) ((TextView) view).getText();
        } else {
            return "";
        }
    }

    public static String getText(View rootView, @IdRes int id) {
        View view = findBy(rootView, id);
        if (view instanceof TextView) {
            Object obj = ((TextView) view).getText();
            if (obj instanceof Editable) {
                return obj.toString();
            } else {
                return (String) ((TextView) view).getText();
            }
        } else {
            return "";
        }
    }


    public static void recyclerViewScroll(RecyclerView recyclerView, CommonRowClick<Boolean> callBack) {
        if (null == recyclerView || callBack == null) {
            return;
        }
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    // 空闲···
                    callBack.onRow(false);
                } else {
                    // 滚动···
                    callBack.onRow(true);
                }
            }
        });
    }

}
